﻿using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp.Exception;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace SketchUpAPI.NET.Objects
{
    /// <summary>
    /// 参考点，就是在 SketchUp 中使用卷尺工具创建的一个参考点
    /// </summary>
    /// <remarks>对 SketchUpAPI 中的 SUGuidePointRef 的包装</remarks>
    public class GuidePoint : DrawingElement
    {
        #region API 函数
        /// <summary>
        /// 使用坐标创建一个参考点
        /// </summary>
        /// <param name="guide_point"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUGuidePointCreate(out IntPtr guide_point, Point3D position);

        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="guide_point"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUGuidePointRelease(ref IntPtr guide_point);

        /// <summary>
        /// 获取参考点的坐标
        /// </summary>
        /// <param name="guide_point"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUGuidePointGetPosition(IntPtr guide_point, ref Point3D position);

        /// <summary>
        /// 获取参考点的起始点
        /// </summary>
        /// <remarks>如果该对象是在SketchUp中创建的，则起始点是在创建过程中第一次单击的位置。如果是使用SUGuidePointCreate创建的，则起始点为原点。</remarks>
        /// <param name="guide_point"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUGuidePointGetFromPosition(IntPtr guide_point, ref Point3D position);

        /// <summary>
        /// 是否显示为线段
        /// </summary>
        /// <param name="guide_point"></param>
        /// <param name="as_line"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUGuidePointGetDisplayAsLine(IntPtr guide_point, out bool as_line);
        #endregion

        /// <summary>
        /// 是否显示为线段
        /// </summary>
        /// <remarks>false：仅显示参考点本身；true：显示由起始点与参考点相连的一段虚线线段</remarks>
        public bool IsDiaplayAsLine
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUGuidePointGetDisplayAsLine(Id, out bool as_line));
                return as_line;
            }
        }

        /// <summary>
        /// 参考点位置
        /// </summary>
        public Point3D Position
        {
            get
            {
                Point3D p = Point3D.Origin;
                SketchUpExceptionHelper.ThrowSUResult(SUGuidePointGetPosition(Id, ref p));
                return p;
            }
        }

        /// <summary>
        /// 起始点
        /// </summary>
        public Point3D FromPosition
        {
            get
            {
                Point3D p = Point3D.Origin;
                SketchUpExceptionHelper.ThrowSUResult(SUGuidePointGetFromPosition(Id, ref p));
                return p;
            }
        }

        internal GuidePoint(IntPtr id) => Id = id;

        /// <summary>
        /// 创建一个参考点
        /// </summary>
        /// <param name="position"></param>
        /// <returns></returns>
        public static GuidePoint Create(Point3D position)
        {
            SketchUpExceptionHelper.ThrowSUResult(SUGuidePointCreate(out IntPtr id, position));
            return new GuidePoint(id);
        }

        public override void Dispose()
        {
            SketchUpExceptionHelper.ThrowSUResult(SUGuidePointRelease(ref Id));
        }
    }
}